import { create, remove, update, query, removeUser, byrole, addUser,grantrole,Accountquery,listRole,EditUser } from '../services/permission'
import { parse } from 'qs'
import { isEmptyObject } from '../utils'

export default {

  namespace: 'cbd',

  state: {
    list: [],
    rolelist:[],
    AmountNumber:[],
    AmountBolon:[],
    editdatarole:[],
    currentItem: {},
    alluserlist:[],
    allrole:[],
    addroleId:0,
    allsecele:[],
    roleTitle:'add',
    modalVisible: false,
    modalType: 'create',
    roleadd:true,
    StyleRole:false,
    StyleUser:false,
    StyleEdituser:false,
    StyleRoleview:false,
    IndexListStyle:true,
    isMotion: localStorage.getItem('antdAdminUserIsMotion') === 'true',
    pagination: {
      showSizeChanger: true,
      showQuickJumper: true,
      showTotal: total => `共 ${total} 条`,
      current: 1,
      total: null,
    },
    userpagination:{
      showSizeChanger: true,
      showQuickJumper: true,
      showTotal: total => `共 ${total} 条`,
      current: 1,
      total: null,
    }
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname === '/permission') {
          dispatch({
            type: 'query',
            payload: location.query,
          })
          dispatch({
            type: 'showIndexList',
          })
        }
      })
    },
  },
  effects: {
    *query ({ payload }, { call, put }) {
        yield put({ type: 'showLoading' })
        var page = {};  
        if(isEmptyObject(payload.page)){
          page = {total:0,current:1,pageSize:10,field:payload.field,keyword:payload.keyword}
        }else{
          page = {current:payload.page,pageSize:payload.pageSize,field:payload.field,keyword:payload.keyword}
        }
        const data = yield call(query, parse(page))
        if (data) {
          yield put({
            type: 'querySuccess',
            payload: {
              list: data.data.rows,
              AmountNumber:data.data.params.adminAmount,
              pagination: {total:data.data.total,current:data.data.offset + 1,pageSize:(data.data.pageSize)},
            }
          })
        }
    },
    
    *hideIndexList ({ payload }, { call, put }) {
        const data = yield call(byrole,payload)
        if (data) {
          yield put({ type: 'hideRole' })
          yield put({
            type: 'hideIndexListStyle',
            payload: {
              AmountBolon:data.data.rows.params,
              addroleId:payload.roleId,
              rolelist: data.data.rows.rows,
              userpagination:{
                total:0,current:0,pageSize:0
              },
              roleTitle:payload.name.name,
              allsecele:payload.name
            }
          })
        }
    },
     *byrole ({ payload }, { call, put }) {
       console.log("payload==",payload)
        yield put({ type: 'showLoading' })
        const data = yield call(byrole, parse(payload.id))
        if (data) {
          yield put({ type: 'hideRole' })
          yield put({
            type: 'byroleSuccess',
            payload: {
              AmountBolon:data.data.rows.params,
              addroleId:payload.id.roleId,
              rolelist: data.data.rows.rows,
              userpagination:{
                total:0,current:0,pageSize:0
              },
              roleTitle:payload.name.name,
              allsecele:payload.name
            }
          })
        }
    },
    *Account ({ payload }, { call, put }) {
        yield put({ type: 'showLoading' })
        var page = {};
        if(payload.current === true){
          page = {total:0,current:1,pageSize:10,field:payload.field,keyword:payload.keyword}
        }else{
          page = {current:payload.current,pageSize:payload.pageSize,field:payload.field,keyword:true}
        }
        const data = yield call(Accountquery, parse(page))
        if (data) {
          yield put({ type: 'hideRole' })
          yield put({
            type: 'byroleSuccess',
            payload: {
              addroleId:0,
              AmountBolon:data.data.params,
              rolelist: data.data.rows,
              userpagination:{
                total:data.data.total,current:data.data.offset + 1,pageSize:(data.data.pageSize)
              },
              roleTitle:'全部用户'
            }
          })
        }
    },
    *grantrole ({ payload }, { call, put }) {
        yield put({ type: 'showLoading' })
        const data = yield call(byrole, parse(payload))
        if (data) {
          yield put({ type: 'hideRole' })
          yield put({
            type: 'byroleSuccess',
            payload: {
              addroleId:payload.roleId,
              AmountBolon:data.data.rows.params,
              rolelist: data.data.rows.rows,
            }
          })
        }
    },
    *'DeleteRole' ({ payload }, { call, put }) {
      const data = yield call(remove, { id: payload.data })
      if (data.data.status == 200) {
        const page = {total:payload.pagination.pagination,current:payload.pagination.current,pageSize:payload.pagination.pageSize}
        const data = yield call(query, parse(page))
        if (data) {
          yield put({
            type: 'querySuccess',
            payload: {
              AmountNumber:data.data.params.adminAmount,
              list: data.data.rows,
              pagination: {total:data.data.total,current:data.data.offset + 1,pageSize:(data.data.pageSize)}
            }
          })
        }
      }
    },
    *'delete'({payload},{call,put}){
      const data = yield call(removeUser,{id:payload.id})
      if (data.data.status == 200) {
        const data = yield call(byrole, payload)
        if (data) {
          yield put({ type: 'hideRole' })
          yield put({
            type: 'byroleSuccess',
            payload: {
              AmountBolon:data.data.rows.params,
              rolelist: data.data.rows.rows,
            }
          })
        }
      }
    },
    *showRole({ payload },{call, put }){
      let title = ''
      let data = {}
      const roleidata={roleId:payload.data.id}
      const alluserlist = yield call(grantrole,parse(roleidata))
      if(payload.roleTitle=='add'){
        title ='新增角色'
        data={
          roleTitle:title,
          alluserlist:alluserlist.data.rows,
          editdatarole:{},
        }
      }else if(payload.roleTitle=='copy'){
        title ='新增角色'
        data={
          roleTitle:title,
          alluserlist:alluserlist.data.rows,
          editdatarole:{},
        }
      }else{
        title =payload.data.name
         data={
          roleTitle:title,
          alluserlist:alluserlist.data.rows,
          editdatarole:payload.data,
          addroleId:payload.data.id
        }
      }
      yield put({ 
        type: 'showRoles',
        payload:data
       })
    },
    *viwePer({ payload },{call, put }){
       const datas = yield call(grantrole,{roleId:payload.id})
       const data={
          roleTitle:payload.name,
          alluserlist:datas.data.rows,
          addroleId:payload.id,
        }
        if(datas.data.status ==200){
          yield put({ 
            type: 'showroleper',
            payload:data
          })
        }
    },
    *hideRole({ payload },{call, put }){
        yield put({ type: 'hideRoles' })
    },
    *showUser({ payload },{call, put }){
      const data = yield call(listRole)
      if(data){
        yield put({
          type: 'showUsers',
          payload: {
            modalType:'create',
            allrole: data.data.rows,
          }
        })
      }
    },
    *showEdit({ payload },{call, put }){
        yield put({
          type: 'showEditUs',
          payload: {
            currentItem: payload.currentItem,
            modalType:payload.modalType,
            allrole:[]
          }
        })
    },
    *create ({ payload }, { call, put }) {
      const data = yield call(create, payload)
      if (data.data.status == 200) {
        const page = {total:0,current:1,pageSize:10}
        const data = yield call(query, parse(page))
        if (data) {
          yield put({ type: 'hideRole' })
          yield put({
            type: 'querySuccess',
            payload: {
              AmountNumber:data.data.params.adminAmount,
              list: data.data.rows,
            }
          })
        }
      }
    },
    *update ({ payload }, { select, call, put }) {
      const data = yield call(update, payload)
      if (data.data.status == 200) {
        const page = {total:0,current:1,pageSize:10}
        const data = yield call(query, parse(page))
        if (data) {
          yield put({ type: 'hideRole' })
          yield put({
            type: 'querySuccess',
            payload: {
              list: data.data.rows,
            }
          })
        }
      }
    },
    *EditUser ({ payload }, { select, call, put }) {
      const data = yield call(EditUser, payload.data)
      if (data.data.status == 200) {
        const data = yield call(byrole, payload)
        if (data) {
          yield put({ type: 'hideRole' })
          yield put({
            type: 'byroleSuccess',
            payload: {
              AmountBolon:data.data.rows.params,
              rolelist: data.data.rows.rows,
            }
          })
        }
      }
    },
    *addUser ({ payload }, { call, put }) {
      const data = yield call(addUser, payload.data)
      console.log("payload==",payload)
      if (data.data.status == 200) {
        const payloads = {roleId:payload.data.roleId}
        const data = yield call(byrole, parse(payloads))
        const page = {total:payload.pagination.pagination,current:payload.pagination.current,pageSize:payload.pagination.pageSize}
        const datas = yield call(query, parse(page))
        yield put({ type: 'hideRole' })
        if (data) {
          yield put({
            type: 'byroleSuccess',
            payload: {
              AmountBolon:data.data.rows.params,
              rolelist: data.data.rows.rows,
              AmountNumber:datas.data.params.adminAmount,
              list: datas.data.rows,
            }
          })
        }
      }
    },
    *switchIsMotion ({
      payload,
    }, { put }) {
      yield put({
        type: 'handleSwitchIsMotion',
      })
    },
  },

  reducers: {
    querySuccess (state, action) {
      const { list,AmountNumber, pagination} = action.payload
      return { ...state,
        list,
        AmountNumber,
        pagination: {
          ...state.pagination,
          ...pagination,
        } }
    },
    byroleSuccess (state, action) {
      const { rolelist, addroleId} = action.payload
      return { ...state,
        ...action.payload,
       }
    },
    hideIndexListStyle(state, action) {
      const { rolelist, addroleId} = action.payload
      return { ...state,
        ...action.payload,
        IndexListStyle:false,
       }
    },
    showIndexList(state, action){
      return { ...state, IndexListStyle: true }
    },
    showModal (state, action) {
      return { ...state, ...action.payload, modalVisible: true }
    },
    showRoles (state, action) {
      return { ...state, ...action.payload,roleadd: false,StyleRole:true,StyleUser:false,IndexListStyle:false,StyleEdituser:false,StyleRoleview:false }
    },
    hideRoles (state, action) {
      return { ...state, ...action.payload, roleadd: true,StyleRole:false,StyleUser:false,StyleEdituser:false,StyleRoleview:false }
    },
    showUsers (state, action) {
      return { ...state, ...action.payload, roleadd: false,StyleUser:true,IndexListStyle:false,StyleRole:false,StyleEdituser:false ,StyleRoleview:false }
    },
    showEditUs (state, action) {
      return { ...state, ...action.payload, roleadd: false,StyleUser:false,StyleRole:false,StyleEdituser:true ,StyleRoleview:false }
    },
    showroleper (state, action) {
      return { ...state, ...action.payload, roleadd: false,StyleUser:false,StyleRole:false,StyleEdituser:false ,StyleRoleview:true }
    },
    hideModal (state) {
      return { ...state, modalVisible: false }
    },
    handleSwitchIsMotion (state) {
      localStorage.setItem('antdAdminUserIsMotion', !state.isMotion)
      return { ...state, isMotion: !state.isMotion }
    },
  },

}
